package com.xuecheng.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.media.model.dto.MediaAuditDTO;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.enums.common.AuditEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.media.common.constant.MediaErrorCode;
import com.xuecheng.media.entity.Media;
import com.xuecheng.media.mapper.MediaMapper;
import com.xuecheng.media.service.MediaService;
import com.xuecheng.media.service.OperationMediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * <p>
 * 媒资信息 服务实现类
 * </p>
 *
 * @author 黄子弟
 */
@Slf4j
@Service
public class OperationMediaServiceImpl extends ServiceImpl<MediaMapper, Media> implements OperationMediaService {

    @Value("${aliyun.region}")
    private String regionId;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    @Autowired
    private MediaService mediaService;

    private static final Long companyId = AuditEnum.COMPANY_ID;

    @Override
    public PageVO<MediaDTO> queryMediaList(PageRequestParams params, QueryMediaModel model) {
        PageVO<MediaDTO> mediaDTOS = mediaService.queryMediaList(params, model, companyId);

        return mediaDTOS;
    }

    @Override
    @Transactional
    public void modifyAuditStatus(MediaAuditDTO mediaAuditDTO) {
        //关键数据
        if (ObjectUtils.isEmpty(mediaAuditDTO.getId())) {
            ExceptionCast.cast(MediaErrorCode.E_140002);
        }

        if (ObjectUtils.isEmpty(mediaAuditDTO.getAuditMind())) {
            ExceptionCast.cast(MediaErrorCode.E_140002);
        }

        if (ObjectUtils.isEmpty(mediaAuditDTO.getAuditStatus())) {
            ExceptionCast.cast(MediaErrorCode.E_140002);
        }
        //业务数据
        LambdaQueryWrapper<Media> wrapper = Wrappers.lambdaQuery(Media.class);
        wrapper.eq(Media::getId, mediaAuditDTO.getId());
        Media media = this.getOne(wrapper);
        if (com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isEmpty(media)) {
            ExceptionCast.cast(MediaErrorCode.E_140002);
        }



        String auditStatus = media.getAuditStatus();
        if ((auditStatus.equals(AuditEnum.AUDIT_PASTED_STATUS.getCode()))) {
            ExceptionCast.cast(MediaErrorCode.E_140002);
        }

        //只能赋值审核状态和审核未通过两种状态
        String auditStatu = mediaAuditDTO.getAuditStatus();
        if (auditStatu.equals(AuditEnum.AUDIT_UNPAST_STATUS.getCode())) {
            ExceptionCast.cast(MediaErrorCode.E_140002);
        }

        //修改课程
        LambdaUpdateWrapper<Media> updateWrapper = Wrappers.lambdaUpdate(Media.class);
        updateWrapper
                .set(Media::getAuditStatus, auditStatu)
                .set(Media::getChangeDate,LocalDateTime.now())
                .set(Media::getAuditMind, mediaAuditDTO.getAuditMind())
                .eq(Media::getId, mediaAuditDTO.getId());

        boolean update = mediaService.update(updateWrapper);
        if (!update) {
            ExceptionCast.cast(MediaErrorCode.E_140002);
        }
    }

    @Override

    public String previewMedia(Long mediaId) {
        String url = mediaService.previewMedia(mediaId, companyId);
        return url;
    }


}
